{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-02-10T15:25:56.569459Z",
     "start_time": "2025-02-10T15:25:44.202916Z"
    }
   },
   "source": [
    "import random\n",
    "import time\n",
    "import numpy as np\n",
    "\n",
    "#随机1亿个数据\n",
    "a = []\n",
    "for i in range(100000000):\n",
    "    a.append(random.random())\n",
    "print('随机完毕')"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机完毕\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-10T15:26:06.855498Z",
     "start_time": "2025-02-10T15:26:02.252670Z"
    }
   },
   "cell_type": "code",
   "source": [
    "b = np.array(a)  # 转换为ndarray\n",
    "print('转换完毕')"
   ],
   "id": "dfc2527d044274a0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "转换完毕\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-10T15:26:12.620345Z",
     "start_time": "2025-02-10T15:26:11.758709Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#ndarray的计算效率优于python的list\n",
    "t1 = time.time()\n",
    "sum1 = sum(a)\n",
    "t2 = time.time()\n",
    "\n",
    "t4 = time.time()\n",
    "sum3 = np.sum(b)\n",
    "t5 = time.time()\n",
    "print(t2 - t1, t5 - t4)"
   ],
   "id": "74754b3115d6010c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7506484985351562 0.10825753211975098\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-11T03:37:27.059765Z",
     "start_time": "2025-02-11T03:37:27.030709Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.array([1, 2, 3])\n",
    "print(t1)\n",
    "print(type(t1))"
   ],
   "id": "adabee9d7804ac79",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-11T03:37:31.938848Z",
     "start_time": "2025-02-11T03:37:31.933101Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(type(range(10)))  # range返回的是一个range对象，不能直接转换为ndarray\n",
    "t2 = np.array(range(10))\n",
    "print(t2)\n",
    "print(type(t2))"
   ],
   "id": "aabe2347da33626e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'range'>\n",
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-11T03:38:08.663072Z",
     "start_time": "2025-02-11T03:38:08.655415Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#二维列表转ndarray\n",
    "import numpy as np\n",
    "\n",
    "list2 = [[1, 2], [3, 4], [5, 6]]\n",
    "\n",
    "twoArray = np.array(list2)\n",
    "print(type(twoArray))\n",
    "print(twoArray)\n",
    "print(list2)  #列表的输出是有逗号的，ndarray的输出是没有逗号的"
   ],
   "id": "30426a9a0e5a57f0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[[1, 2], [3, 4], [5, 6]]\n"
     ]
    }
   ],
   "execution_count": 6
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
